package yaq.mini.weibo.leafwb.service;

import org.springframework.stereotype.Service;
import yaq.mini.weibo.leafwb.mapper.*;
import yaq.mini.weibo.leafwb.pojo.Article;
import yaq.mini.weibo.leafwb.pojo.FavouriteCollection;
import yaq.mini.weibo.leafwb.pojo.Picture;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private PictureMapper pictureMapper;
    @Resource
    private FavouriteCollectionMapper favouriteCollectionMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private KeyWordMapper keyWordMapper;

    public List<Article> listArticles() {
        List<Article> articleList = new ArrayList<>();
        try {
            articleList = articleMapper.listFullArticle();
            for (int i = 0; i < articleList.size(); i++) {
                if (articleList.get(i).getSharedUser().getIsAllowShare().equalsIgnoreCase("F")) {
                    articleList.remove(i);
                    i--;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return articleList;
        }
        return articleList;
    }

    public List<Article> getNewestArticle(int count) {
        List<Article> articleList = new ArrayList<>();
        try {
            articleList = articleMapper.listFullArticleOrderByTimeLimit(count);
        } catch (Exception e) {
            e.printStackTrace();
            return articleList;
        }
        return articleList;
    }

    public List<Article> listArticlesOrderByTime() {
        List<Article> articleList = new ArrayList<>();
        try {
            articleList = articleMapper.listFullArticleOrderByTime();
        } catch (Exception e) {
            e.printStackTrace();
            return articleList;
        }
        return articleList;
    }

    public List<Article> listArticlesByUserID(String userId) {
        List<Article> articleList = new ArrayList<>();
        try {
            articleList = articleMapper.listFullArticleByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return articleList;
        }
        return articleList;
    }

    public List<Article> listArticlesByKey(String key) {
        List<Article> articleList = new ArrayList<>();
        try {
            articleList = articleMapper.listFullArticleByContent(key);
        } catch (Exception e) {
            e.printStackTrace();
            return articleList;
        }
        return articleList;
    }

    /**
     * 根据文章编号得到文章
     *
     * @param articleIds
     * @return
     */
    public List<Article> listArticlesByID(List<String> articleIds) {
        List<Article> articleList = new ArrayList<>();
        for (int i = articleIds.size() - 1; i >= 0; i--) {
            Article article = articleMapper.getFullArticleByID(articleIds.get(i));
            articleList.add(article);
        }
        articleList = sortArticleBytime(articleList);
        return articleList;
    }

    public Article getArticleByID(String articleId) {
        Article article = new Article();
        try {
            article = articleMapper.getFullArticleByID(articleId);
        } catch (Exception e) {
            e.printStackTrace();
            return article;
        }
        return article;
    }

    public boolean insertArticleByShare(Article article, List<String> urls) {
        try {
            articleMapper.insert(article);
            for (String url : urls) {
                String pictureId = UUID.randomUUID() + "";
                Picture picture = new Picture(pictureId, article.getArticleId(), url);
                pictureMapper.insert(picture);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean updateArticleByLike(String userId, String articleId, String isLike) {
        try {
            FavouriteCollection favouriteCollection = favouriteCollectionMapper.getFavouriteCollectionByID(userId, articleId);
            FavouriteCollection newFC = new FavouriteCollection(userId, articleId, isLike, "F", "F");
            if (favouriteCollection == null) {
                favouriteCollectionMapper.insert(newFC);
            } else {
                favouriteCollectionMapper.updateLike(newFC);
            }
            Long like = (long) ("T".equalsIgnoreCase(isLike) ? 1 : -1);
            articleMapper.updateByLikeNum(articleId, like);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean updateArticleByCollect(String userId, String articleId, String isCollect) {
        try {
            FavouriteCollection favouriteCollection = favouriteCollectionMapper.getFavouriteCollectionByID(userId, articleId);
            FavouriteCollection newFC = new FavouriteCollection(userId, articleId, "F", isCollect, "F");
            if (favouriteCollection == null) {
                favouriteCollectionMapper.insert(newFC);
            } else {
                favouriteCollectionMapper.updateCollect(newFC);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean updateArticleByComment(String articleId, int count) {
        try {
            articleMapper.updateByCommentCount(articleId, count);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean updateArticleByAuthority(String articleId, int authority) {
        try {
            articleMapper.updateByAuthority(articleId, authority);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteArticleByAId(String articleId) {
        try {
            pictureMapper.deleteByAID(articleId);
            commentMapper.deleteByAID(articleId);
            favouriteCollectionMapper.deleteByAID(articleId);
            articleMapper.deleteArticleByID(articleId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 判断字符串是否含不合法得字符
     *
     * @param content 字符串
     * @return
     */
    public boolean isContentIllegal(String content) {
        boolean isLegal = true;
        try {
            List<String> keywords = keyWordMapper.listKeyWord();
            for (String keyword : keywords) {
                if (content.contains(keyword)) {
                    isLegal = false;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return isLegal;
        }
        return isLegal;
    }

    /**
     * 用冒泡排序对文章按时间先后进行排序
     *
     * @param articleList
     * @return
     */
    public List<Article> sortArticleBytime(List<Article> articleList) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            for (int i = 0; i < articleList.size() - 1; i++) {
                for (int j = 0; j < articleList.size() - i - 1; j++) {
                    Date bt = sdf.parse(articleList.get(j).getReleaseTime());
                    Date et = sdf.parse(articleList.get(j + 1).getReleaseTime());
                    if (bt.before(et)) {
                        Article article = articleList.get(j);
                        articleList.set(j, articleList.get(j + 1));
                        articleList.set(j + 1, article);
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return articleList;
        }
        return articleList;
    }

    /****************关键字增删查改****************/

    public List<String> listKeyword() {
        List<String> keywordList = new ArrayList<>();
        try {
            keywordList = keyWordMapper.listKeyWord();
        } catch (Exception e) {
            e.printStackTrace();
            return keywordList;
        }
        return keywordList;
    }

    public boolean updateKeyword(String keyword, String newKeyword) {
        boolean isSuccess = true;
        try {
            String str = keyWordMapper.isKeyWord(newKeyword);
            if (str == null || str.length() == 0)
                keyWordMapper.update(keyword, newKeyword);
            else
                isSuccess = false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isSuccess;
    }

    public boolean deleteKeyword(String keyword) {
        try {
            keyWordMapper.delete(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean addKeyword(String keyword) {
        boolean isSuccess = true;
        try {
            String str = keyWordMapper.isKeyWord(keyword);
            if (str == null || str.length() == 0)
                keyWordMapper.insert(keyword);
            else
                isSuccess = false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isSuccess;
    }
}
